import {promptBox,toast} from 'nc-lightapp-front';

export default class PubAction{
    constructor(comp){
        this.comp = comp
    }



    //操作模版
    initMeta =(template)=>{
        let _this = this
        let items = template['handcondgrid'].items
        items.push({
            attrcode: 'opr',
            itemtype: 'customer',
            label: this.comp.props.main.json['gx6008-000046'], /* 国际化处理： 操作*/
            width: '200px',
            textAlign: 'center',
            visible: true,
            fixed: 'right',
            render(text, record, index) {
                const {props} = _this.comp
                const {main} = props
                return(
                    <span>
                        <a style={{ cursor: 'pointer'}}
                            onClick={() => {
                               _this.showFormPage('edit',record.values.pk_handcond.value)
                            }}
                        >{main.json['gx6008-000038']}</a>{/* 国际化处理： 修改*/}
                        <a style={{ cursor: 'pointer',paddingLeft:20 }}
                            onClick={() => {
                                promptBox(
                                    {
                                        color: "warning",
                                        title: main.json['gx6008-000018'], /* 国际化处理： 提示*/
                                        content: main.json['gx6008-000019'], /* 国际化处理： 确定删除所选数据*/
                                        beSureBtnClick: function () {
                                            props.dispatch({
                                                type: 'main/delTableData',
                                                payload: {
                                                    pk_handconds: record.values.pk_handcond.value
                                                }
                                            }).then((res)=>{
                                                if(res.data === 'success'){
                                                    _this.getTableData(main.orgvalue.refpk,main.type,main.pageInfo)
                                                } 
                                        })
                                    }
                                })
                               
                            }}
                        >{main.json['gx6008-000039']}</a>{/* 国际化处理： 删除*/}
                    </span>
                )
            }
        })
        let subItems = template['handitemsetgrid'].items
        subItems.push({
            attrcode: 'opr',
            itemtype: 'customer',
            label: this.comp.props.main.json['gx6008-000046'], /* 国际化处理： 操作*/
            width: '200px',
            textAlign: 'center',
            visible: true,
            fixed: 'right',
            render(text, record, index) {
                const {props} = _this.comp
                const {main} = props
                let number = props.editTable.getAllRows('handitemsetgrid').length
                return(
                    <span>
                        <a style={{ cursor: 'pointer',color: index !== 0?'#007ace': '#d0d0d0',pointerEvents: index === 0?'none': 'auto',
                                    display: main.cardstatus === 'browse' ? 'none': ''}}
                            onClick={() => {
                                props.editTable.setValByKeyAndIndex('handitemsetgrid',index,'orders',{value:index})
                                props.editTable.setValByKeyAndIndex('handitemsetgrid',index-1,'orders',{value:index+1})
                                props.editTable.moveRow('handitemsetgrid',index,index-1)
                            }}
                        >{main.json['gx6008-000072']}</a>{/* 国际化处理： 上移*/}
                        <a style={{ cursor: 'pointer',paddingLeft:20,color: index !== number-1 ?'#007ace': '#d0d0d0',
                                    pointerEvents: index !== number-1?'auto':'none',display: main.cardstatus === 'browse' ? 'none': ''}}
                            onClick={() => {
                                props.editTable.setValByKeyAndIndex('handitemsetgrid',index,'orders',{value:index+2})
                                props.editTable.setValByKeyAndIndex('handitemsetgrid',index+1,'orders',{value:index+1})
                                props.editTable.moveRow('handitemsetgrid',index,index+1)
                            }}
                        >{main.json['gx6008-000073']}</a>{/* 国际化处理： 下移*/}
                    </span>
                )
            }
        })
    }

    getTableData =(value={},type='1',pageInfo)=>{
        const{props} = this.comp
        const{main} = props
        props.dispatch({
            type: 'main/getTableData',
            payload:{
                type: type,
                pk_org: value.refpk || main.orgvalue.refpk,
                pageInfo: pageInfo
            }
        }).then((res)=>{
            let pageInfo = {
                pageIndex: 1,
                pageSize: 10,
                total: 0,
            }
            if(res.data && res.data.handcondgrid){
                if(type === '2'){
                    props.editTable.setTableData('dishandcondgrid',res.data.handcondgrid)
                }else{
                    props.editTable.setTableData('handcondgrid',res.data.handcondgrid)
                }
                pageInfo =  res.data.handcondgrid.pageInfo
            }else{
                if(type === '2'){
                    props.editTable.setTableData('dishandcondgrid',{rows:[]})
                }else{
                    props.editTable.setTableData('handcondgrid',{rows:[]})
                }
            }
            props.dispatch({
                type: 'main/update',
                payload: {
                   pageInfo : pageInfo, 
                }
            })
        })
    }


    showFormPage =(status,pk_handcond)=>{
        const{props} = this.comp
        const{main} = props
        props.dispatch({
            type: 'main/getFormValue',
            payload: {
                pk_handcond: pk_handcond,
                type: main.type
            }
        }).then((res)=>{
            if(res.data){
                props.editTable.setStatus('handitemsetgrid',status)
                props.editTable.setTableData('handitemsetgrid',res.data.grid && res.data.grid.handitemsetgrid)
                props.form.setAllFormValue({'handcondform':res.data.form && res.data.form.handcondform})
                props.button.setButtonVisible({
                    'edit': status === 'edit' ? false: true,
                    'save': status !== 'edit' ? false: true,
                    'cancel': status !== 'edit' ? false: true,
                })
                props.dispatch({
                    type: 'main/update',
                    payload: {
                        showPage: 'card',
                        cardstatus: status
                    }
                })
            }
           
        })
    }

}